﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using SQLServer;
using Zhang.IDAL.UCenter.Login;
using Zhang.Model.UCenter;

namespace  Zhang.SQLServer.UCenter.Login
{
    public class Login : ILogin
    {
        public int Register(Model.UCenter.Login model)
        {
            StringBuilder sbInsert;
            List<SqlParameter> lstParam;
            int newID;
            object objNewID;

            lstParam = new List<SqlParameter>();
            sbInsert = new StringBuilder();

            sbInsert.Append("INSERT INTO[USER]([UserName],[Pwd],[Mobile],[Mail],[LastLoginTime]");
            sbInsert.Append(") VALUES(@UserName,@Pwd,@Mobile,@Mail,@LastLoginTime);");
            sbInsert.Append("SELECT SCOPE_IDENTITY();");
            lstParam.Add(new SqlParameter("@UserName",model.UserName??""));
            lstParam.Add(new SqlParameter("@Pwd", model.Pwd ?? ""));
            lstParam.Add(new SqlParameter("@Mobile", model.Mobile ?? ""));
            lstParam.Add(new SqlParameter("@Mail", model.Mail ?? ""));
            lstParam.Add(new SqlParameter("@LastLoginTime", model.LastLoginTime??DateTime.Now));

            objNewID = DBHelper.ExecuteScalar(CommandType.Text, sbInsert.ToString(), lstParam.ToArray());
            newID = objNewID != null && !string.IsNullOrEmpty(objNewID.ToString()) ? int.Parse(objNewID.ToString()) : 0;
            return newID;

        }

        /// <summary>
        /// 获取会员信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public User GetByKey(Model.UCenter.Login Login)
        {
            User model= new User(); 
            var param = GetSqlWhere(Login);
            string sql = "select * from [User]"+param.Where;
            using (SqlDataReader sqlReader = DBHelper.ExecuteReader(CommandType.Text, sql, param.LstParam.ToArray()))
            {
                if (sqlReader.Read())
                {
                    model.ID= !string.IsNullOrEmpty(sqlReader["ID"].ToString()) ? int.Parse(sqlReader["ID"].ToString()) : 0;
                    model.UserName = sqlReader["UserName"].ToString();
                    model.Mail= sqlReader["Mail"].ToString();
                    model.Mobile = sqlReader["Mobile"].ToString();
                    model.Pwd = sqlReader["Pwd"].ToString();
                }
                sqlReader.Close();
                sqlReader.Dispose();
            }

            return model;
        }

        /// <summary>
        /// 查询条件 筛选
        /// </summary>
        /// <param name="model">查询条件</param>
        /// <returns></returns>
        private static global::SQLServer.QueryParam GetSqlWhere(Model.UCenter.Login model)
        {
            var param = new global::SQLServer.QueryParam() { LstParam = new List<SqlParameter>(), Where = string.Empty };
            if (!string.IsNullOrEmpty(model.UserName))
            {
                param.Where += !string.IsNullOrEmpty(param.Where) ? " AND " : "";
                param.Where += " UserName = @UserName  ";
                param.LstParam.Add(new SqlParameter("@UserName", model.UserName));
            }

            param.Where = string.IsNullOrEmpty(param.Where) ? param.Where : " WHERE " + param.Where;
            return param;
        }
    }
}
